package com.yanqu.road.server.cmd.cross.player;

import com.yanqu.road.entity.player.*;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.PlayerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_TO_WATCH_PLAYER, desc = "跨服拜访玩家")
public class CrossWatchPlayerCmd extends AbstractCommand {

	private long userId;

	private long serverId;

	private long watchUserId;

	private long watchServerId;

	private WatchPlayerResult watchPlayerResult;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PlayerProto.CrossWatchPlayerReqMsg reqMsg = PlayerProto.CrossWatchPlayerReqMsg.parseFrom(packet.getBytes());
		userId = reqMsg.getUserId();
		serverId = reqMsg.getServerId();
		watchUserId = reqMsg.getWatchUserId();
		watchServerId = reqMsg.getWatchServerId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		watchPlayerResult = UserMgr.watchPlayer(watchUserId);
	}

	@Override
	public void sendData(GamePlayer player) {
		PlayerProto.CrossWatchPlayerSimpleRespMsg.Builder respMsg = PlayerProto.CrossWatchPlayerSimpleRespMsg.newBuilder();
		respMsg.setUserId(userId);
		respMsg.setServerId(serverId);
		//拜访的数据改了
//		PlayerProto.WatchPlayerRespMsg.Builder playMsg = PlayerPb.parseWatchPlayerRespMsg(watchPlayerResult);
		PlayerProto.WatchPlayerSimpleRespMsg.Builder playMsg = PlayerPb.parseWatchPlayerSimpleRespMsg(watchPlayerResult);
		respMsg.setRespData(playMsg);
		YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_WATCH_PLAYER_RESP, respMsg);
		GamePlayerMgr.sendPacket(0, pbMsg);
	}
}
